package joney.main.views;

import joney.main.action.IAccountsChanged;
import joney.main.action.ITransactionOccured;
import joney.main.utils.FormatDecimal;
import joney.main.utils.JoneyUtil;
import joney.model.Account;
import joney.model.DataModel;
import joney.model.Expense;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.part.ViewPart;

public class AccountsOverview extends ViewPart implements PaintListener, ITransactionOccured , IAccountsChanged{

	public static String ID= "joney.accountsoverview";
	
	private Canvas canvas;
	private Display display = JoneyUtil.getDisplay();
	
	public void createPartControl(Composite parent) {

		//listener for transaction events
		DataModel.addTransactionListener(this);
		DataModel.addAccountsListener(this);
		
		canvas = new Canvas(parent,SWT.DOUBLE_BUFFERED);
		canvas.addPaintListener(this);
		
		
	}

	public void setFocus() {
		canvas.redraw();
	}

	public void paintControl(PaintEvent e) {
	
		GC g2d = e.gc;
		
		int offset = 50;
		int width = e.width;
		int height = e.height;
		
		//fill color of whole pane
		g2d.setBackground(JoneyUtil.getDisplay().getSystemColor(SWT.COLOR_WHITE));
		g2d.fillRectangle(0,0,width, height);
		
		//g2d.setFont(new Font(JoneyUtil.getDisplay(), "Arial", 11, SWT.NORMAL));
		
		g2d.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
		g2d.fillRectangle(10, 15, width -10, height-70);
		
		//g2d.setForeground(display.getSystemColor(SWT.COLOR_BLUE));
		
		g2d.drawRectangle(8, height -40, 110, 25);
		g2d.drawString("Net: ", 11, height -32);
		
		double net =0;
		//get net money
		for(Account a: DataModel.accounts)
		{
			net += (a.getAmount() + a.getStartingAmount());
		}
		
		String nets = new Double(FormatDecimal.format( 2, net)).toString();
		g2d.drawString(nets, 40, height - 32);
		
		//draw the accounts to the sidebar
		int i=0;
		for(Account a: DataModel.accounts)
		{
			g2d.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
			g2d.drawString(a.getName(), 15, 50 + (i*offset));
			
			
			Double d = new Double(FormatDecimal.format(2, a.getAmount() + a.getStartingAmount()));
			if(d.doubleValue() < 0)
			{
				g2d.setForeground(display.getSystemColor(SWT.COLOR_RED));
			}
			else
				g2d.setForeground(display.getSystemColor(SWT.COLOR_BLUE));
			
			
			g2d.drawString("$ " + d.toString(), 15, 70 + (i*offset));
			g2d.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
			g2d.drawLine(13, 40 + (i*offset), 108, 40 + (i*offset));
			i++;
		}
		
		
	}

	public void expenseAdded(Expense e, String a) {
		this.canvas.redraw();
	}

	public void expenseRemoved(Expense e, String a) {
		this.canvas.redraw();
	}

	public void transferOccured(String to, String from, Expense e, Expense e2) {
		this.canvas.redraw();
	}

	public void finishedLoadingExpenses() {
		this.canvas.redraw();
		
	}

	public void accountAdded(Account a) {
		this.canvas.redraw();
	}

	public void accountRemoved(Account a) {
		this.canvas.redraw();
	}

	public void expensesUpdated() {
		this.canvas.redraw();
	}

}
